package nuc.jyg.controller;

import nuc.jyg.bean.User;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * @Author Nuc YongGuang Ji
 * Created by JiYongGuang on 2017/5/10.
 */
@Controller
@RequestMapping("/hello")
public class HelloMvcController {

    Logger logger = LoggerFactory.getLogger(HelloMvcController.class);

    /*
    *   params parameter test
    *   ModelAndView 返回参数的测试
    *
    * */
    @RequestMapping(value = "/display", params = "username", method = RequestMethod.POST)
    public ModelAndView helloMvc(HttpServletRequest httpServletRequest, Model model) {
        logger.info("param1" + httpServletRequest.getRequestURI() + "|| param2" + httpServletRequest.getRequestURL());
        logger.info(httpServletRequest.getParameter("username"));
        logger.info(httpServletRequest.getContextPath());
        return new ModelAndView("/home");
    }

    /*
    *   前台 User Entity request params 的 binding
    *   request forword And request response Test
    *
    * */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String doSave(User user, Model model, HttpServletRequest httpServletRequest) {
        /*
            如果映射方法中需要 绑定式接收 传输过来的数据到 User 对象。可以直接在参数中加上一个User对象。
            SpringMVC 会自动绑定参数
            这样就必须保证前台表单中的input标签的id属性的值要和User对象中的属性名一致。这样就完成了数据绑定
        */
        System.out.println("前台来的 user 对象的 username 参数 " + httpServletRequest.getParameter("username"));

        logger.info(ReflectionToStringBuilder.toString(user));

        model.addAttribute(user);
        return "forward:display?username=" + user.getUsername();
//        return "redirect:mvc/" + user.getPassword();
    }

    /*
    *   file upload Test
    *
    * */
    @RequestMapping(value = "/doUpload", method = RequestMethod.POST)
    public String doUploadFile(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        if (!multipartFile.isEmpty()) {
            logger.info("11111", multipartFile.getOriginalFilename());
            FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), new File("E:\\testSpringmvcMultipartFile\\", System.currentTimeMillis() + multipartFile.getOriginalFilename()));
        }
        return "course/success";
    }


    /*
    *   @ResponseBody annoation Test
    *
    * */
    @RequestMapping(value = "/zet/{courseID}/{qwe}", method = RequestMethod.GET)
    public @ResponseBody
    User getUser(@PathVariable Integer qwe,
                 @PathVariable Integer courseID) {
        User user = new User();
        user.setUsername("jiyongguang");
        user.setPassword("jiyongguang!");
        logger.info("参数" + courseID + qwe);
        return user;
    }

    /*
    *   Generated Return Json Body From ResponseEntity
    *
    * */
    @RequestMapping(value = "/index/{index}", method = RequestMethod.GET)
    public ResponseEntity<User> getUser2(@PathVariable Integer index) {
        User user = new User();
        user.setUsername("liyanan");
        user.setPassword("0723!");
        logger.info("参数" + index);
        return new ResponseEntity<User>(user, HttpStatus.OK);
    }


    /*
    *   Put Request Method Test From Form
    *
    * */
    @RequestMapping(value = "/testPut", method = RequestMethod.PUT)
    @ResponseBody
    public String testGet() {
        System.out.println(1);
        return "putMethod";
    }

    /*
    *   Delete Request Method From From
    *
    * */
    @RequestMapping(value = "/testDelete", method = RequestMethod.DELETE)
    @ResponseBody
    public String testDelete() {
        System.out.println(2);
        return "deleteMethod";
    }

    /*
    *   At prasent Contnroller's ErrorHandler Class
    *
    * */
    @ExceptionHandler
    public ModelAndView exceptionHandler(Exception e) {
        ModelAndView modelAndView = new ModelAndView("error");
        modelAndView.addObject("exception", e);
        return modelAndView;
    }

    /*
    *   Error Trigger Class
    *
    * */
    @RequestMapping("/error")
    public String err() {
        int i = 5 / 0;
        return "hello";
    }

}


